/*
 * Copyright (c) 2008 Openviewing
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Created:            09-Sep-2006 15:12:43
 */
package org.openviewing.foundation.control.factory.properties;

import java.util.Enumeration;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openviewing.foundation.control.factory.template.BeanFactoryTemplate;

/** Concrete bean factory.
 *
 *  This class reads a property files to create beans.
 */
public class PropertiesBeanCreator extends BeanFactoryTemplate
{
    private static final    String COMMAND_PREFIX = "Bean";
    private static final    String SEPARATOR      = ".";
    private                 Log    log            = null;

    /** Bean registration
     *
     *  This adds the bean to the registry.
     *
     *  @param    name    The reference name of the bean used through out the application.
     *  @param    type    The class type of the bean.
     *  @see    org.arcanenexus.foundation.factory.BeanRegistryStrategy#addBean(String, String) Bean Factory
     */
    public final void addBean(final String name, final String type)
    {
        this.beanDefinitions.put(name, type);
    }
    
    // Constructors ////////////////////////////////////////////////////////////
    /** Default Constructor. */
    public PropertiesBeanCreator()
    {
        super();
        this.log    = LogFactory.getLog(this.getClass().getName());
    }
    /** Properties constructor
     *
     *  Constructs the registry based on the properties. The format of the properties is:
     *  bean.<i>name</i>=<i>classpath.class.name</i>
     *
     *  @param properties A <code>java.util.Properties</code> containing the bean definitions.
     */
    @SuppressWarnings("unchecked")
	public PropertiesBeanCreator(final Properties properties)
    {
        this();

        Enumeration<String>	names       = null;
        String        		key         = null;
        int            		separator	= -1;
        String        		beanKey     = null;
        String        		beanValue   = null;

        names = (Enumeration<String>) properties.propertyNames();
        while (names.hasMoreElements())
        {
            key = (String) names.nextElement();
            this.log.debug("Processing :" + key);
            if(key.startsWith(COMMAND_PREFIX))
            {
                this.log.debug("Is bean");
                separator = key.indexOf(SEPARATOR);
                if(separator > -1)
                {
                    beanKey = key.substring(separator + 1);
                    beanValue = (String) properties.get(key);
                    this.addBean(beanKey, beanValue);
                    this.log.debug(this.beanDefinitions.toString());
                }
            }
        }
    }
}
